package com.alipay.app.groupmeal.standard.widget.business;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StyleableRes;

/**
 * @ProjectName: 团餐
 * @ClassName: BaseBScreenView
 * @Description: B屏
 * @Author: bd-001935
 * @CreateDate: 4/14/22 9:41 AM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/14/22 9:41 AM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public abstract class BaseBScreenView extends LinearLayout {

    private Context mContext;
    private View mView;

    public BaseBScreenView(Context context) {
        this(context, null);
    }

    public BaseBScreenView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseBScreenView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        getAttrs(context, attrs);
        init();
        initData();
    }

    private void init() {
        mView = LayoutInflater.from(mContext).inflate(getLayoutId(), this);
        initView(mView);
        initData();
    }

    @LayoutRes
    protected abstract int getLayoutId();

    @StyleableRes
    public int[] getStyleableRes() {
        return null;
    }

    protected abstract void initView(View view);

    protected abstract void initData();

    @SuppressLint("ResourceType")
    private void getAttrs(Context context, AttributeSet attrs) {
        if (getStyleableRes() != null && getStyleableRes().length > 0) {
            TypedArray ta = context.obtainStyledAttributes(attrs, getStyleableRes());
            getAttrs(ta);
            ta.recycle();
        }
    }

    public void getAttrs(TypedArray ta) {

    }

    public boolean isVisibility() {
        return getVisibility() == VISIBLE;
    }

    public void show() {
        if (!isVisibility()) {
            post(new Runnable() {
                @Override
                public void run() {
                    setVisibility(VISIBLE);
                }
            });
        }
    }

    public void dismiss() {
        if (isVisibility()) {
            post(new Runnable() {
                @Override
                public void run() {
                    setVisibility(GONE);
                }
            });
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                return onKeyEventBack(event);
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                return onKeyEventEnter(event);
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
                    || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
                return onKeyEventUpAndDown(event);
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_SETTINGS) {
                return onKeyEventSetting(event);
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_F1) {
                return onKeyEventFunction(event);
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_0
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_1
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_2
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_3
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_4
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_5
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_6
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_7
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_8
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_9
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_DOT
                    || event.getKeyCode() == KeyEvent.KEYCODE_DEL
                    || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ADD) {
                return onKeyEventNum(event);
            }
        }
        return super.dispatchKeyEvent(event);
    }

    /**
     * 取消键
     */
    public boolean onKeyEventBack(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 确认键
     */
    public boolean onKeyEventEnter(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 数字
     */
    public boolean onKeyEventNum(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 向上向下
     */
    public boolean onKeyEventUpAndDown(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 设置
     */
    public boolean onKeyEventSetting(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 功能
     */
    public boolean onKeyEventFunction(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

}
